﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.Text;
using System.IO;
using System.Xml.Serialization;
using System.Xml;

namespace TCDC_System.Content
{
    public partial class TestService : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            HttpWebRequest req = WebRequest.Create("http://localhost:2030/Service/Register") as HttpWebRequest;
            req.KeepAlive = false;
            req.Method = "POST";

            string xml = "<RegisterModel xmlns=\"http://schemas.datacontract.org/2004/07/Model\"> " +
                         "<address>53</address>" +
                         "<birth_date>16/09/1980</birth_date>" +
                         "<country>Thai</country>" +
                         "<education>None</education>" + 
                         "<email>p_channgam@hotmail.com</email>" +
                         "<fname_en>Pattarawut</fname_en>" +
                         "<fname_th>ภัทรวุธ</fname_th>" +
                         "<gender>M</gender>" +
                         "<id_card>12345</id_card>" +
                         "<lname_en>Channgam</lname_en>" +
                         "<lname_th>จันทร์งาม</lname_th>" +
                         "<password>1234</password>" +
                         "<post_code>11000</post_code>" +
                         "<province>None</province>" +
                         "<reference_no>R1234</reference_no>" +
                         "</RegisterModel>";

            Model.RegisterModel r = new Model.RegisterModel();

            
            r.address = "53";
            r.birth_date = "";
            r.country = "Thai";
            r.education = "None";
            r.email = "p_channgam@hotmail.com";
            r.fname_en = "Pattarawut";
            r.fname_th = "";
            r.gender = "M";
            r.id_card = "";

            


            var stringwriter = new System.IO.StringWriter();
            var serializer = new XmlSerializer(r.GetType());
            serializer.Serialize(stringwriter, r);
            string xml2 = stringwriter.ToString();

                        //byte[] buffer = Encoding.ASCII.GetBytes(xml);
                        byte[] buffer = Encoding.UTF8.GetBytes(xml);
                        req.ContentLength = buffer.Length;
                        req.ContentType = "text/xml";

                        Stream PostData = req.GetRequestStream();
                        PostData.Write(buffer, 0, buffer.Length);
                        PostData.Close();

                        HttpWebResponse resp = req.GetResponse() as HttpWebResponse;


                        //Encoding enc = System.Text.Encoding.GetEncoding(1252);
                        StreamReader loResponseStream = new StreamReader(resp.GetResponseStream(), new UTF8Encoding(true, true));

                        string Response = loResponseStream.ReadToEnd();

                        loResponseStream.Close();
                        resp.Close();

                        Label1.Text = xml2;
        }

        
    }
}